﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Data.Linq;

using Ample.Model;

namespace Ample.DAL
{
    public class OrderItemManager : DBBase
    {
        public static OrderItemManager Create()
        {
            return new OrderItemManager();
        }

        #region Select Methods

        public IEnumerable<OrderItem> GetOrderItems()
        {
            using (AmpleDataContext context = DataContext)
            {
                return context.AMPLE_OrderItems_SelectManual().ToArray<OrderItem>();
            }
        }

        //public OrderItemManager GetOrderItemManagerById(int orderItemManagerId)
        //{
        //    using (AmpleDataContext context = DataContext)
        //    {
        //        DataLoadOptions dlo = new DataLoadOptions();
        //        dlo.LoadWith<OrderItemManager>(p => p.OrderItemManagerType);
        //        context.LoadOptions = dlo;
        //        return context.AMPLE_OrderItemManagers_Select_By_OrderItemManagerIdManual(orderItemManagerId).SingleOrDefault<OrderItemManager>();
        //    }
        //}

        //public IEnumerable<OrderItemManager> GetOrderItemManagerByTypeId(int prodTypeId)
        //{
        //    using (AmpleDataContext context = DataContext)
        //    {
        //        DataLoadOptions dlo = new DataLoadOptions();
        //        dlo.LoadWith<OrderItemManager>(p => p.OrderItemManagerType);
        //        context.LoadOptions = dlo;
        //        return context.am_OrderItemManagers_Select_By_OrderItemManagerTypeIdManual(prodTypeId).ToArray<OrderItemManager>();
        //    }
        //}

        #endregion

        #region Update Methods

        public OperationStatus UpdateOrderItem(OrderItem orderItem)
        {
            AmpleDataContext context = null;
            try
            {
                context = DataContext;
                context.AMPLE_OrderItems_Update(
                    orderItem.OrderItemId,
                    orderItem.OrderId,
                    orderItem.ProductId,
                    orderItem.Line,
                    orderItem.Title,
                    orderItem.UnitPrice);
            }
            catch (Exception exp)
            {
                return new OperationStatus(false, exp.Message, exp);
            }
            finally
            {
                if (context != null) context.Dispose();
            }
            return new OperationStatus(true, null, null);
        }


        #endregion

        #region Insert Methods

        public OperationStatus InsertOrderItem(OrderItem orderItem)
        {
            AmpleDataContext context = null;
            int newordId = -1; 
            try
            {
                context = DataContext;
                newordId = context.AMPLE_OrderItems_Insert(
                    orderItem.OrderId,
                    orderItem.ProductId,
                    orderItem.Line,
                    orderItem.Title,
                    orderItem.UnitPrice);

            }
            catch (Exception exp)
            {
                return new OperationStatus(false, exp.Message, exp);
            }
            finally
            {
                if (context != null) context.Dispose();
            }
            return new OperationStatus(true, null, null, newordId);
        }

        #endregion

        #region Delete Methods

        public OperationStatus DeleteOrderItem(OrderItem orderItem)
        {
            AmpleDataContext context = null;
            try
            {
                context = DataContext;
                context.AMPLE_OrderItems_Delete(orderItem.OrderItemId);
            }
            catch (Exception exp)
            {
                return new OperationStatus(false, exp.Message, exp);
            }
            finally
            {
                if (context != null) context.Dispose();
            }
            return new OperationStatus(true, null, null);
        }

        #endregion

    }
}
